package cn.dankal.flyaudio.adapter;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremyliao.liveeventbus.LiveEventBus;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.pojo.CommentsEntity;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.image.PicUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.store.EvaluateListActivity;
import cn.dankal.flyaudio.view.StarView;

/**
 * <pre>
 *     e-mail : junfeng.nie@duntech.com.cn
 *     time   : 21/7/2020
 *     desc   :
 *     version: cn.dankal.flyaudio.adapter
 *     Copyright: Copyright（c）2018
 *     Company:
 * </pre>
 *
 * @author niejunfeng
 */
public class CommentsListAdapter extends BaseRecyclerViewAdapter<CommentsEntity> {
    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_evaluate;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new CommentsListAdapter.ViewHolder(rootView);
    }

    class ViewHolder extends BaseRecyclerViewHolder<CommentsEntity> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(CommentsEntity data, int position) {
            ImageView itemIvAvatar = itemView.findViewById(R.id.item_iv_avatar);
            TextView itemTvContent = itemView.findViewById(R.id.item_tv_content);
            TextView itemTvName = itemView.findViewById(R.id.item_tv_name);
            TextView itemTvTime = itemView.findViewById(R.id.item_tv_time);
            StarView ratingBar = itemView.findViewById(R.id.ratingBar);
            ImageView ivLike = itemView.findViewById(R.id.iv_like);

            RecyclerView itemRvImg = itemView.findViewById(R.id.item_rv_img);

            itemRvImg.setLayoutManager(new GridLayoutManager(context, 3, GridLayoutManager.VERTICAL,
                    false));
            EvaluateImgAdapter evaluateImgAdapter = new EvaluateImgAdapter();
            itemRvImg.setAdapter(evaluateImgAdapter);
            if(data.getImgList()!=null){
                evaluateImgAdapter.addDatas(data.getImgList(),true);
            }
            String avatar = "";
            String name = "";
            if (data.getAppUser() != null) {
                avatar = data.getAppUser().getAvatar();
                name = data.getAppUser().getUsername();
            }
            if("true".equals(data.getiLike())){
                ivLike.setImageResource(R.mipmap.ic_awesome);
            }else{
                ivLike.setImageResource(R.mipmap.ic_awesome_un);
            }
            PicUtils.loadAvatar(avatar, itemIvAvatar);
            itemTvContent.setText(data.getContent());
            itemTvName.setText(name);
            itemTvTime.setText(data.getCreatedDatetime());
            setText(R.id.tv_like_num, "已有" + data.getLikeCount() + "人点赞");
            ivLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LiveEventBus.get("Like", int.class).post(position);
                }
            });


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("uuid", data.getUuid());
                    ActivityUtils.startActivity(bundle, EvaluateListActivity.class);
                }
            });
            switch (data.getScoreType()) {
                case "POOR":
                    ratingBar.setMark(1f);
                    break;
                case "AVERAGE":
                    ratingBar.setMark(2f);
                    break;
                case "GOOD":
                    ratingBar.setMark(3f);
                    break;
            }
        }
    }
}